/**
 *
 */
package com.evil.DAO;

import com.evil.Bean.ItemListBean;
import com.evil.DB_Utils.DB_Utils;
import com.evil.DB_Utils.QueryCallBack;
import com.evil.Data.Data;
import com.evil.Utils.Log;
import com.evil.Utils.PropertiesUtils;

import java.util.ArrayList;

/******************************************
 * 工程名：EdaSupport<br>
 * 包名：com.evil.DAO<br>
 * 时间：2016年7月31日 上午9:44:18<br>
 * 作者：白雨濃 evil<br>
 * 邮箱：evilbai@foxmail.com<br>
 * 内容：<br>
 ******************************************/
public class ItemListDAO
{
    private String BREAKSTRING = new PropertiesUtils().get("BREAKSTRING");

    public ArrayList<ItemListBean> findAllList(String phone, int lastItemId)
    {
       /* String url = "SELECT\n" + "	itemlist.item_id,\n" + "	itemlist.img,\n"
                + "	itemlist.title,\n" + "	itemlist.answer_ids,\n"
                + "	itemlist.type,\n" + "	itemlist.flow,\n"
                + "	itemlist.enabled,\n" + "	itemlist.create_time,\n"
                + "	itemlist.over_time\n" + "FROM\n" + "	itemlist\n"
                + "WHERE\n" + "	itemlist.item_id > ?\n"
                + "AND itemlist.item_id NOT IN (\n" + "	SELECT\n"
                + "		useroveritem.item_id\n" + "	FROM\n" + "		useroveritem\n"
                + "	WHERE\n" + "		useroveritem.phone = ?\n" + ")";*/

        String url = "SELECT\n" +
                "	itemlist.item_id,\n" +
                "	itemlist.img,\n" +
                "	itemlist.title,\n" +
                "	itemlist.answer_ids,\n" +
                "	itemlist.type,\n" +
                "	itemlist.flow,\n" +
                "	itemlist.enabled,\n" +
                "	itemlist.create_time,\n" +
                "	itemlist.over_time\n" +
                "FROM\n" +
                "	itemlist\n" +
                "WHERE\n" +
                "	itemlist.item_id > ?\n" +
                "AND itemlist.item_id NOT IN (\n" +
                "	SELECT\n" +
                "		useroveritem.item_id\n" +
                "	FROM\n" +
                "		useroveritem\n" +
                "	WHERE\n" +
                "		useroveritem.phone = ?\n" +
                ")";

        ArrayList<ItemListBean> list = new ArrayList<ItemListBean>();
        DB_Utils.query(url).add(lastItemId).add(phone).build()
                .getAll(next ->
                {
                    ItemListBean bean = new ItemListBean();
                    bean.setId(next.getInt(1));
                    bean.setImgPath(next.getString(2));
                    bean.setTitle(next.getString(3));

                    String[] ss = next.getString(4).split(BREAKSTRING);
                    bean.setAnswer_ids(ss);

                    bean.setType(next.getInt(5));
                    bean.setFlow(next.getInt(6));

                    if (next.getInt(7) == 1)
                        bean.setEnabled(true);
                    else
                        bean.setEnabled(false);

                    bean.setCreateTime(next.getDateTime(8));
                    bean.setOverTime(next.getDateTime(9));

                    list.add(bean);
                }).close();

        return list;
    }

    /**
     * 根据流量类型返回列表
     *
     * @param phone
     * @param lastItemId
     * @param flowtype
     * @return
     */
    public ArrayList<ItemListBean> findFlowTypeList(String phone, int lastItemId, int flowtype)
    {
        String url = "SELECT\n" +
                "	itemlist.item_id,\n" +
                "	itemlist.img,\n" +
                "	itemlist.title,\n" +
                "	itemlist.answer_ids,\n" +
                "	itemlist.type,\n" +
                "	itemlist.flow,\n" +
                "	itemlist.enabled,\n" +
                "	itemlist.create_time,\n" +
                "	itemlist.over_time\n" +
                "FROM\n" +
                "	itemlist\n" +
                "WHERE\n" +
                "	itemlist.item_id > ?\n" +
                "AND itemlist.flow =?\n" +
                "AND itemlist.item_id NOT IN (\n" +
                "	SELECT\n" +
                "		useroveritem.item_id\n" +
                "	FROM\n" +
                "		useroveritem\n" +
                "	WHERE\n" +
                "		useroveritem.phone = ?\n" +
                ")";
        ArrayList<ItemListBean> list = new ArrayList<ItemListBean>();

        DB_Utils.query(url)
                .add(lastItemId)
                .add(flowtype)
                .add(phone)
                .build()
                .getAll(next ->
                {
                    ItemListBean bean = new ItemListBean();
                    bean.setId(next.getInt(1));
                    bean.setImgPath(next.getString(2));
                    bean.setTitle(next.getString(3));

                    String[] ss = next.getString(4).split(BREAKSTRING);
                    bean.setAnswer_ids(ss);

                    bean.setType(next.getInt(5));
                    bean.setFlow(next.getInt(6));

                    if (next.getInt(7) == 1)
                        bean.setEnabled(true);
                    else
                        bean.setEnabled(false);

                    bean.setCreateTime(next.getDateTime(8));
                    bean.setOverTime(next.getDateTime(9));

                    list.add(bean);
                }).close();

        return list;
    }

}
